// pages/my/gongxiang/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {  
    navbar: ['闲置', '图书'],  
    currentTab: 0,
    index:0,
    list:[],
    pagenum:1
  },
  navbarTap: function(e){  
    this.setData({  
      currentTab: e.currentTarget.dataset.idx,
      list:[],
      pagenum:1
    })  
    this.getDataList();
  },
  showXianZhiInfo1:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'/pages/push/push?type=0&id='+id
    })
  },
  showBookInfo1:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'/pages/push/push?type=1&id='+id
    })
  },
    //下架图书
    xiajiabook:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        var status = e.currentTarget.dataset.status;
        wx.request({
            url: getApp().data.server+'/index.php/Tongcheng/Book/xiajiabook?id='+id+'&upstatus='+status,
            success:function(d){
                if(0 == d.data){
                    wx.showToast({
                        title:'操作失败',
                        icon: 'success',
                        duration: 2000
                    })
                }else{
                    wx.showToast({
                        title:'操作成功',
                        icon: 'success',
                        duration: 2000
                    })
                }
                setTimeout(function(){
                    that.setData({
                        list:[],
                        pagenum:1
                    })
                    that.getDataList();
                },1000)

            }
        })
    },
    //下架闲置
    xiajiaunuse:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        var status = e.currentTarget.dataset.status;
        wx.request({
            url: getApp().data.server+'/index.php/Tongcheng/Book/xiajiaunuse?id='+id+'&status='+status,
            success:function(d){
                if(0 == d.data){
                    wx.showToast({
                        title:'操作失败',
                        icon: 'success',
                        duration: 2000
                    })
                }else{
                    wx.showToast({
                        title:'操作成功',
                        icon: 'success',
                        duration: 2000
                    })
                }

                setTimeout(function(){
                    that.setData({
                        list:[],
                        pagenum:1
                    })
                    that.getDataList();
                },1000)
            }
        })
    },

    /**
     * 删除闲置
     * @param e
     */
    delunuse:function(e){
      var that = this;
      var id = e.currentTarget.dataset.id;
        wx.showModal({
            title:'提示',
            content:'确定删除吗？',
            showCancel:true,
            success:function(res){
                if (res.confirm) {
                  wx.request({
                      url:getApp().data.server+'/index.php/Tongcheng/Book/delUnuse?id='+id,
                      success:res=>{
                          if(0 == res.data){
                              wx.showToast({
                                  title:'删除失败',
                                  icon: 'success',
                                  duration: 2000
                              })
                          }else{
                              wx.showToast({
                                  title:'删除成功',
                                  icon: 'success',
                                  duration: 2000
                              })
                          }
                          setTimeout(function(){
                              that.setData({
                                  list:[],
                                  pagenum:1
                              })
                              that.getDataList();
                          },1000)
                      }
                  })
                }
            }
        })
    },
    /**
     * 删除图书
     * @param e
     */
    delbook:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;

        wx.showModal({
            title:'提示',
            content:'确定删除吗？',
            showCancel:true,
            success:function(res){
                if (res.confirm) {
        wx.request({
            url:getApp().data.server+'/index.php/Tongcheng/Book/delBook?id='+id,
            success:res=>{
                if(0 == res.data){
                    wx.showToast({
                        title:'删除失败',
                        icon: 'success',
                        duration: 2000
                    })
                }else{
                    wx.showToast({
                        title:'删除成功',
                        icon: 'success',
                        duration: 2000
                    })
                }
                setTimeout(function(){
                    that.setData({
                        list:[],
                        pagenum:1
                    })
                    that.getDataList();
                },1000)

            }
        })
                }
            }
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      that.setData({
          currentTab: 1
      })
    this.getDataList();

  },
onShow:function(e){
    var that = this;
    // that.setData({
    //     list:[],
    //     pagenum:1,
    //     currentTab: 1
    // })

},
  //获取用户共享信息
  getDataList:function(e){
    var that = this;
        wx.request({
            url: getApp().data.server+'/index.php/Tongcheng/Book/getGongXiangListByUid?type='+that.data.currentTab+'&pagenum='+that.data.pagenum+'&uid='+getApp().globalData.userInfo.uid,
            success:function(d){
                wx.hideLoading();
                // console.log('123'+JSON.stringify(d.data));
                if(0 == d.data){
                    return;
                }
                that.setData({
                    list:that.data.list.concat(d.data)
                })
            }
        })
  },
    showXianZhiInfo:function(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/pages/index/xianzhiinfo?id='+id
        })
    },
    showBookInfo:function(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/pages/find/bookinfo2_info?id='+id
        })
    }

    ,
    queRenGuiHuan:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        wx.showModal({
            title:'提示',
            content:'确定已经归还吗？',
            showCancel:true,
            success:function(res){
                if (res.confirm) {
                    wx.request({
                        url:getApp().data.server+'/index.php/Tongcheng/Book/queRenGuiHuan?id='+id,
                        success:res=>{
                            if(0 == res.data){
                                wx.showToast({
                                    title:'确认失败',
                                    icon: 'success',
                                    duration: 2000
                                })
                            }else{
                                wx.showToast({
                                    title:'确认成功',
                                    icon: 'success',
                                    duration: 2000
                                })
                            }
                            setTimeout(function(){
                                that.setData({
                                    list:[],
                                    pagenum:1
                                })
                                that.getDataList();
                            },1000)

                        }
                    })
                }
            }
        })
  },
    //指定到订单页面
    toOrderInfo:function(e){
      var that = this;
      var id = e.currentTarget.dataset.orderid;
      wx.navigateTo({
        url: '/pages/order/orderInfoMai?id='+id
      })
    },
    //指定到图书订单页面
    bookOrderInfo:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/find/sureJieMai?id='+id
        })
    },
    onReachBottom: function (e) {
        // var that = this;
        // // 显示加载图标
        // wx.showLoading({
        //     title: '玩命加载中',
        // })
        // that.setData({
        //     pageNum:1,
        //     xieWenData:[]
        // })
        // this.getDataList();
    },
    onPullDownRefresh:function(e){
        // console.log('下拉刷新')
        wx.showLoading({
            title: '玩命加载中',
        })
        var that = this;
        that.setData({
            pagenum:that.data.pagenum+1

        })
        this.getDataList();
    },
})